<?php


namespace app\controller;

use app\model\AlbumPhoto;

/**
 * 相册
 * @package app\controller
 */
class Albums extends BackendController{
    protected $model = null;
    public function _initialize()
    {
        //parent::_initialize();
        $this->model = new \app\model\Albums();
    }

    public function sys_lists(){
        $this->model->sys_lists();
        $this->success('同步成功');
    }

    public function add(){
        $params = $this->request->post();
        $name=$params['name'];
        $note=$params['note'];
        $level=$params['level'];
        $follow=$params['follow'];
        $res=$this->model->create_album($name,$note,$level,$follow);
        if(!$res){
            $this->success('新增成功');
        }else{
            $this->error($res);
        }

    }

    public function add_photo($album_id,$old,$follow){
        $album=$this->find($album_id);
        if($album){
            $path=config('capture.capture_path').'/album/'.$album_id.'/'.date('Ym').'/'.date('d').'/';
            $service_type=config('capture.platform');
            $service=null;
            eval('$service= new app\service\impl\Albums'.$service_type.'ServiceImpl();');
            $follow_album=$this->where('parent_id',$album_id)->find();

            $post_ai_return=$service->create_face($old,3,$album['album_id']);
            $post_ai_return_array = json_decode($post_ai_return, true);

            if ($post_ai_return_array['code'] === 0) {
                $res = $post_ai_return_array['data'];
                $data['file_path']=$path;
                $data['album_id']=$album_id;
                $data['photo_id']=$res['photo_id'];
                $data['create_at']=date('Y-m-d H:i:s');
                $photo=AlbumPhoto::create($data);
                save_base_img($photo->id,$old,$path);
                save_base_img($photo->id.'_follow',$follow,$path);
                if($follow_album){
                    $follow_return=$service->create_face($follow,3,$follow_album['album_id']);
                    $follow_return=json_decode($follow_return,true);
                    if($follow_return['code']===0){
                        $photo->follow_id=$follow_return['data']['photo_id'];
                        $photo->save();
                    }
                }
            }else{
                $this->error('上传失败');
            }

        }

    }

    public function edit($id = null){
        $params = $this->request->post();
        $name=$params['name'];
        $note=$params['note'];
        $level=$params['level'];
        $this->model->edit_album($id,$name,$note,$level);
    }

    public function del($id = "") {
        $this->model->del_album($id);
    }


}